<template>
    <div class="table">
        <div class="container">
            <div class="handle-box">
                <el-form ref="queryFormData" :model="queryFormData" inline size="small">
                    <el-button type="primary" size="small" :disabled="multipleSelection.length !== 0 ? false : true"
                        icon="el-icon-delete" @click="delAll">批量失效</el-button>
                    <el-form-item label="创建日期" style="margin-left: 10px">
                        <el-date-picker v-model="rangeDateValue" align="center" end-placeholder="结束日期" range-separator="-"
                            start-placeholder="开始日期" type="daterange" unlink-panels>
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="有效性" style="margin-left: 10px">
                        <el-select v-model="queryFormData.dataFlag" style="width: 150px">
                            <el-option :value="0" label="有效" />
                            <el-option :value="1" label="无效" />
                        </el-select>
                    </el-form-item>
                    <el-button size="small" icon="el-icon-search" @click="search">搜索</el-button>
                </el-form>
            </div>
            <el-table v-loading="loading" :data="tableData" size="mini" style="width: 100%"
                @selection-change="handleSelectChanged">
                <el-table-column type="selection" :selectable="selectable" />
                <el-table-column label="歌单图片" width="110" align="center">
                    <template slot-scope="{row}">
                        <div class="song-list-img">
                            <img :src="row.pic" style="width: 100%;" />
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="title" label="标题" width="170" align="center" />
                <el-table-column prop="stylesStr" label="流派" width="110" align="center" />
                <el-table-column prop="userName" label="创建人" width="110" align="center">
                    <template slot-scope="{row}">
                        <p class="el-icon-user"> {{ row.userName }}</p>
                    </template>
                </el-table-column>
                <el-table-column prop="createTimeStr" label="创建时间" width="90" align="center" />
                <el-table-column prop="intro" label="简介" align="center">
                    <template slot-scope="{row}">
                        <p style="height: 100px; overflow: scroll">{{ row.intro }}</p>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="歌曲" prop="song" width="100">
                    <template slot-scope="{row}">
                        <el-button size="mini" :disabled="row.dataFlag === 1" @click="content(row.id)"
                            icon="el-icon-headset">歌曲</el-button>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="评论" prop="comment" width="100">
                    <template slot-scope="{row}">
                        <el-button size="mini" :disabled="row.dataFlag === 1" @click="comment(row.id)"
                            icon="el-icon-edit">评论</el-button>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="操作" prop="operation" width="90">
                    <template slot-scope="{row}">
                        <el-button size="mini" :disabled="row.dataFlag === 0 ? false : true" type="danger"
                            @click="move(row.id)">失效</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination @current-change="handleCurrentChange" background layout="prev, pager, next, total"
                    :current-page="pageNum" :page-size="pageSize" :total="total" />
            </div>
        </div>
    </div>
</template>  

<script>
import { mixin } from '../mixins';
import { listSongList, delSongList } from '../api/index';
export default {
    mixins: [mixin],
    data() {
        return {
            tableData: [],
            pageNum: 1,
            pageSize: 10,
            total: 0,
            loading: true,
            multipleSelection: [],
            queryFormData: {
                userId: this.$route.query.userId,
                dataFlag: 0
            },
            rangeDateValue: '',

        }
    },
    mounted() {
        this.getData();
    },
    watch: {
        rangeDateValue: {
            handler: function (val) {
                if (val && val.length === 2) {
                    this.queryFormData.createStartTime = this.dayjs(val[0]).format("YYYY-MM-DD 00:00:00");
                    this.queryFormData.createEndTime = this.dayjs(val[1]).format("YYYY-MM-DD 23:59:59");
                } else {
                    this.queryFormData.createStartTime = '';
                    this.queryFormData.createEndTime = '';
                }
            },
            deep: true,
            immediate: true
        }
    },
    methods: {
        getData() {
            this.loading = true;
            const { queryFormData, pageNum, pageSize } = this;
            listSongList(queryFormData, pageNum, pageSize).then(res => {
                this.loading = false;
                const { data, total, success } = res;
                if (!success) {
                    this.$message.error('查询失败');
                    this.tableData = [];
                    return;
                }
                this.tableData = data.map(e => {
                    return {
                        ...e,
                        toggle: false
                    }
                });
                this.total = total;
            })
        },
        search() {
            this.getData();
        },
        handleCurrentChange(currentPageNum) {
            this.pageNum = currentPageNum;
            this.getData();
        },
        del(ids) {
            delSongList(ids).then(res => {
                const { success } = res;
                if (success) {
                    this.$message.success("操作成功");
                    this.getData();
                } else {
                    this.$message.error("操作失败");
                }
            });
        },
        move(id) {
            this.$confirm('确定失效该记录吗?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.del([id]);
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消操作'
                });
            });
        },
        delAll() {
            this.$confirm('确定批量失效吗?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.del(this.multipleSelection.map(e => e.id));
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消操作'
                });
            });
        },
        selectable(row) {
            return row.dataFlag === 1 ? false : true;
        },
        comment(id) {
            this.$router.push({ path: "/Comment", query: { songListId: id } });
        },
        content(id) {
            this.$router.push({ path: "/Content", query: { songListId: id } });
        },
    }
}

</script>

<style scoped>
.song-list-img {
    width: 100%;
    height: 90px;
    margin-top: 5px;
    margin-bottom: 5px;
    overflow: hidden;
}
</style>